<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="订单标题" prop="title">
				<el-input v-model="form.title" placeholder="订单标题" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="订单编号" prop="orderCode">
						<el-input v-model="form.orderCode" placeholder="订单编号" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="付款方式" prop="payType">
						<sc-select v-model="form.payType" :params="{code:'payType'}" :apiObj="$API.crm.crmDic.getCrmDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="客户名称" prop="customerId">
						<el-select
							v-model="form.customerId"
							filterable
							remote
							reserve-keyword
							placeholder="请输入关键词"
							:remote-method="remoteMethod"
							:loading="loading"
							style="width:100%"
						>
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="报价单号" prop="quotationId">
							<el-select
								v-model="form.quotationId"
								filterable
								remote
								reserve-keyword
								placeholder="请输入关键词"
								:remote-method="remoteQuotationMethod"
								:loading="loading1"
								style="width:100%"
								@change="getCrmQuotationFormJsonById"
							>
								<el-option v-for="item in quotationOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
							</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="采购人员" prop="linkManId">
						<sc-select v-model="form.linkManId" :params="{customerId:form.customerId}" :apiObj="$API.crm.crmLinkMan.getCrmLinkManListForSelect" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="联系电话" prop="tel">
						<el-input v-model="form.tel" placeholder="联系电话" clearable maxlength="40"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="支付日期" prop="payDay" maxlength="80">
						<el-date-picker :disabled-date="publishDateAfter" v-model="form.payDay" placeholder="支付日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="订单金额" prop="amount">
						<el-input-number v-model="form.amount" placeholder="订单金额" clearable></el-input-number>/元
					</el-form-item>
				</el-col>
			</el-row>
			<el-divider content-position="center">订单明细</el-divider>
			<sc-form-table v-model="mx" :addTemplate="addTemplate" drag-sort placeholder="暂无数据">
				<el-table-column prop="productId" label="产品名称" width="200">
					<template #default="scope">
						<el-select
							v-model="scope.row.productId"
							filterable
							remote
							reserve-keyword
							placeholder="请输入关键词"
							:remote-method="remoteProductMethod"
							:loading="loading"
							style="width:100%"
							@change="setOtherValue(scope.row, scope.$index)"
						>
							<el-option v-for="item in productOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</template>
				</el-table-column>
				<el-table-column prop="materialCode" label="产品编码" width="150" :show-overflow-tooltip="true">
					<template #default="scope">
						{{scope.row.materialCode}}
					</template>
				</el-table-column>
				<el-table-column prop="model" label="产品型号" width="100" align="center" :show-overflow-tooltip="true">
					<template #default="scope">
						{{scope.row.model}}
					</template>
				</el-table-column>
				<el-table-column prop="delivery" label="交货期" width="150" align="center">
					<template #default="scope">
						<el-date-picker v-model="scope.row.delivery" placeholder="交货期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</template>
				</el-table-column>
				<el-table-column prop="count" label="数量" width="100">
					<template #default="scope">
						<el-input-number v-model="scope.row.count" clearable controls-position="right" style="width: 80px;" @change="setTotal(scope.row)"></el-input-number>
					</template>
				</el-table-column>
				<el-table-column prop="unit" label="单位" width="100" align="center">
					<template #default="scope">
						<sc-select v-model="scope.row.unit" :params="{code:'crmUnit'}" :apiObj="$API.crm.crmDic.getCrmDicByCode" clearable filterable style="width: 100%"></sc-select>
					</template>
				</el-table-column>
				<el-table-column prop="guidePrice" label="单价" width="150" align="center">
					<template #default="scope">
						<el-input-number v-model="scope.row.guidePrice" placeholder="单价" clearable style="width: 130px;" @change="setTotal(scope.row)" controls-position="right"></el-input-number>
					</template>
				</el-table-column>
				<el-table-column prop="total" label="小计" width="100" align="center">
					<template #default="scope">
						{{scope.row.total}}
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="备注" align="center">
					<template #default="scope">
						<el-input v-model="scope.row.remark" clearable></el-input>
					</template>
				</el-table-column>
			</sc-form-table>
			<el-form-item label="订单备注" style="margin-top: 10px;">
				<el-input type="textarea" rows="4" v-model="form.remark" placeholder="订单备注" clearable maxlength="380"></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'crm'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>

import AttachMenu from "@/views/module/attachMenu";

export default {
	emits: ['success', 'closed'],
	components: {
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg:false,
			loading:false,
			loading1:false,
			fileList:[],
			productOptions:[],
			mx:[],
			addTemplate: {
				productId: '',
				productName:"",
				materialCode: '',
				model: '',
				count: 0,
				unit:"",
				guidePrice:0,
				delivery:"",
				remark: "",
			},
			mode: "add",
			apiObj:this.$API.file.attach.uploadFile,
			titleMap: {
				add: '创建订单记录',
				edit: '编辑订单记录',
			},
			form: {
				orderId:'',
				orderCode:"",
				title: '',
				payType: '',
				customerId: '',
				linkManId:'',
				quotationId:"",
				tel:"",
				payDay:'',
				amount: '',
				remark: '',
				attachId:'',
				mx:""
			},
			options: [],
			quotationOptions:[],
			rules: {
				title: [
					{required: true, message: '订单标题不能为空', trigger: 'blur'}
				],
				orderCode: [
					{required: true, message: '订单编号不能为空', trigger: 'blur'}
				],
				payType:[
					{required: true, message: '支付方式不能为空', trigger: 'blur'}
				],
				customerId:[
					{required: true, message: '客户名称不能为空', trigger: 'blur'}
				],
				linkManId:[
					{required: true, message: '相关人员不能为空', trigger: 'blur'}
				],
				payDay:[
					{required: true, message: '支付日期不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		publishDateAfter(time){
			return time.getTime()<(Date.now()-24*60*60*1000)
		},
		async remoteProductMethod(keyword) {
			let res = await this.$API.crm.crmProduct.getAllProductListForSelect.get({keyword: keyword});
			if (res.code == 200) {
				this.productOptions = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.form.mx = JSON.stringify(this.mx);
					var res;
					if (this.mode == "add") {
						res = await this.$API.crm.crmOrder.insertCrmOrder.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.crm.crmOrder.updateCrmOrderAndMx.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		addNewFile(res, file)
		{
			let tArr =[];
			if(this.form.attachId!=undefined&&this.form.attachId!=null&&this.form.attachId!=''){
				tArr = this.form.attachId.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
		async getAllCustomerListForSelect(customerId) {
			let res = await this.$API.crm.crmCustomer.getAllCustomerListForSelect.get({customerId: customerId});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async remoteMethod(keyword) {
			let res = await this.$API.crm.crmCustomer.getAllCustomerListForSelect.get({keyword: keyword});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		setOtherValue(row,index)
		{
			this.productOptions.forEach(item=>{
				if(item.value==row.productId)
				{
					row.materialCode=item.materialCode;
					row.productName = item.label;
					row.model = item.model;
					row.guidePrice = item.guidePrice;
				}
			})
		},
		async getCrmQuotationFormJsonById(val)
		{
			let res = await this.$API.crm.crmQuotation.getCrmQuotationFormJsonById.get({quotationId: val});
			if (res.code == 200) {
				let listMx = JSON.parse(res.data.formJson);
				let params=[];
				listMx.forEach(item=>{
					params.push(item.productId);
				})
				res = await this.$API.crm.crmProduct.getCrmProductListByIdsForSelect.get({ids:params});
				if (res.code == 200) {
					for(let i=0;i<res.data.length;i++)
						this.productOptions = res.data;
					this.mx = listMx;
				}
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async getCrmQuotationListForSelect(quotationId){
			let res = await this.$API.crm.crmQuotation.getCrmQuotationListForSelect.get({quotationId: quotationId});
			if (res.code == 200) {
				this.quotationOptions = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async remoteQuotationMethod(keyword) {
			let res = await this.$API.crm.crmQuotation.getCrmQuotationListForSelect.get({keyword: keyword});
			if (res.code == 200) {
				this.quotationOptions = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		setData(data) {
			this.getAllCustomerListForSelect(data.customerId);
			if(data.quotationId!=undefined&&data.quotationId!='')
			{
				this.getCrmQuotationListForSelect(data.quotationId);
				this.getCrmQuotationFormJsonById(data.quotationId);
			}
			this.form.orderId = data.orderId
			this.form.title = data.title
			this.form.payType = data.payType
			this.form.payDay = data.payDay
			this.form.orderCode = data.orderCode
			this.form.attachId = data.attachId
			this.form.customerId = data.customerId
			this.form.linkManId = data.linkManId
			this.form.quotationId = data.quotationId
			this.form.tel = data.tel
			this.form.amount = data.amount
			this.form.remark = data.remark
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
